package com.zynn.service.module.es.controller;

import com.zynn.common.core.base.BaseController;
import com.zynn.common.core.utils.ThreadPoolManager;
import com.zynn.common.pojo.result.Result;
import com.zynn.common.pojo.result.ResultUtil;
import com.zynn.service.module.es.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jerry-liqi
 *
 * 初始化数据
 */
@Slf4j
@RestController
@Validated
@RequestMapping("init/data")
public class InitDataController extends BaseController {

    @Autowired
    private InitAllEventService initAllEventService;

    @Autowired
    private InitUserFriendsService initUserFriendsService;

    @Autowired
    private InitVersionSchoolUserFriendsService initVersionSchoolUserFriendsService;

    @Autowired
    private InitProductUserFriendsService initProductUserFriendsService;

    @Autowired
    private InitVersionProfileUserFriendsService initVersionProfileUserFriendsService;

    @RequestMapping(value = "/event", method = RequestMethod.POST)
    public Result initEvent(Integer current, boolean isAsc) {
        ThreadPoolManager.getInstance().execute(()-> initAllEventService.sync(current, isAsc));
        return ResultUtil.success();
    }

    @RequestMapping(value = "/event/segment", method = RequestMethod.POST)
    public Result initEvent(Long start, Long end, boolean isAsc) {
        ThreadPoolManager.getInstance().execute(()-> initAllEventService.init(start, end, isAsc));
        return ResultUtil.success();
    }


    @RequestMapping(value = "/friend", method = RequestMethod.POST)
    public Result initFriend(Integer current, boolean isAsc) {
        ThreadPoolManager.getInstance().execute(()-> initUserFriendsService.initUserFriends(current, isAsc));
        return ResultUtil.success();
    }

    @RequestMapping(value = "/friend/segment", method = RequestMethod.POST)
    public Result initFriend(Long start, Long end, boolean isAsc) {
        ThreadPoolManager.getInstance().execute(()-> initUserFriendsService.init(start, end, isAsc));
        return ResultUtil.success();
    }

    @RequestMapping(value = "/friend/one", method = RequestMethod.POST)
    public Result initFriend(Long userId) {
        initUserFriendsService.initUserFriendsWithWeight(userId);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/friend/product/one", method = RequestMethod.POST)
    public Result initProductFriend(Long userId) {
        initProductUserFriendsService.initPorductUserFriendsWithWeight(userId);
        return ResultUtil.success();
    }

    /**
     * 高校用户推荐权重
     * @param userId
     * @return
     */
    @RequestMapping(value = "/version/school/friend", method = RequestMethod.POST)
    public Result initVersionSchoolUserFriend(@RequestParam("userId") Long userId,@RequestParam(value = "beLikedUserId",required = false) Long beLikedUserId) {
        initVersionSchoolUserFriendsService.initVersionSchoolUserFriendsWithWeight(userId,beLikedUserId);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/friend/profile", method = RequestMethod.POST)
    public Result initVersionProfileUserFriend(@RequestParam("inviteUserId") Long inviteUserId,@RequestParam("beInvitedUserId") Long beInvitedUserId) {
        initVersionProfileUserFriendsService.initVersionProfileUserFriendsWithWeight(inviteUserId,beInvitedUserId);
        return ResultUtil.success();
    }

}
